﻿// <copyright file="OptionsControl.xaml.cs" company="Mooville">
//   Copyright © 2010 Roger Deetz. All rights reserved.
// </copyright>

namespace Mooville.QUno.Store
{
    using System;
    using Mooville.QUno.Store.ViewModel;
    using Mooville.QUno.ViewModel;
    using Windows.ApplicationModel;
    using Windows.Storage;
    using Windows.UI.ApplicationSettings;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;

    public sealed partial class OptionsControl : UserControl
    {
        private OptionsViewModel viewModel;

        public OptionsControl()
        {
            this.InitializeComponent();

            if (!DesignMode.DesignModeEnabled)
            {
                this.viewModel = new OptionsViewModel(new StoreSettingsProvider());
                this.Loaded += this.Control_Loaded;
                this.Unloaded += this.Control_Unloaded;
                this.buttonBack.Click += this.BackButton_Click;
            }
        }

        private void Control_Loaded(object sender, RoutedEventArgs e)
        {
            this.viewModel.LoadSettings();

            return;
        }

        private void Control_Unloaded(object sender, RoutedEventArgs e)
        {
            this.viewModel.SaveSettings();

            return;
        }

        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.Parent.GetType() == typeof(Popup))
            {
                ((Popup)this.Parent).IsOpen = false;
            }

            SettingsPane.Show();

            return;
        }
    }
}
